﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Objects;
using System.Text;

namespace badminton.Controls
{
    public partial class CourtScheduleCtrl : System.Web.UI.UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                int hallId = -1;
                if (this.Request.QueryString["hallid"] != null)
                {
                    hallId = Convert.ToInt32(this.Request.QueryString["hallid"]);
                }
                /////////
                BadmintonEntities bde = new BadmintonEntities();
                ObjectSet<Hall> halls = bde.Hall;
                foreach (Hall hall in halls)
                {
                    this.DropDownList_schedulehall.Items.Add(new ListItem(hall.name, hall.id.ToString()));
                }

                //////////
                DateTime dt = DateTime.Now;
                this.TextBox_scheduledate.Text = dt.ToString("yyyy.MM.dd");

                if (hallId != -1)
                {
                    this.DropDownList_schedulehall.SelectedValue = hallId.ToString();
                    bindSchedule(hallId, dt);
                }
                else if (halls.Count<Hall>() > 0)
                {
                    Hall h = halls.FirstOrDefault();
                    bindSchedule(h.id, dt);
                }
            }


        }

        void bindSchedule(int hallId, DateTime dt)
        {
            BadmintonEntities bde = new BadmintonEntities();
            Hall hall = bde.Hall.FirstOrDefault(p => p.id == hallId);
            if (hall != null)
            {

                this.ListView1.DataSource = hall.Court;
                this.ListView1.DataBind();
            }


        }

        protected void OnScheduleDateChanged(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Parse(this.TextBox_scheduledate.Text).Date;
            if (this.DropDownList_schedulehall.Items.Count > 0)
            {
                int hallid = Convert.ToInt32(this.DropDownList_schedulehall.SelectedItem.Value);
                bindSchedule(hallid, dt);
            }

        }

        protected void OnScheduleHallChanged(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Parse(this.TextBox_scheduledate.Text).Date;
            if (this.DropDownList_schedulehall.Items.Count > 0)
            {
                int hallid = Convert.ToInt32(this.DropDownList_schedulehall.SelectedItem.Value);
                bindSchedule(hallid, dt);
            }
        }

        public string GetDetailSchedule(object id)
        {

            StringBuilder sTemp = new StringBuilder();

            sTemp.Append("<select>");

            sTemp.Append("<option>");
            sTemp.Append("详细信息");
            sTemp.Append("</option>");

            DateTime dtstart, dtend;
            if (TextBox_scheduledate.Text.Trim().Length >= 0)
            {
                dtstart = DateTime.Parse(this.TextBox_scheduledate.Text);
                dtend = dtstart.AddDays(1);
            }
            else
            {
                dtstart = DateTime.Now.Date;
                dtend = dtstart.AddDays(1);
            }

            int courtId = (int)id;
            BadmintonEntities bde = new BadmintonEntities();
            IQueryable<CourtSchedule> query = bde.CourtSchedule.Where(p => p.courtId == courtId && p.startTime > dtstart && p.startTime < dtend).OrderBy(p => p.startTime);
            List<CourtSchedule> sds = query.ToList();
            foreach (CourtSchedule cs in sds)
            {               
                sTemp.Append("<option>");
                sTemp.Append(cs.startTime.Value.ToString("hh:mm") + "-" + cs.endTime.Value.ToString("hh:mm"));
                sTemp.Append("  "+cs.CourtScheduleType.name);
                sTemp.Append("</option>");
            }

            sTemp.Append("</select>");


　          return sTemp.ToString();
        }



        public string GetSchedule(object obj)
        {
            int unit = 18;

            DateTime dtstart, dtend;
            if (TextBox_scheduledate.Text.Trim().Length >= 0)
            {
                dtstart = DateTime.Parse(this.TextBox_scheduledate.Text);
                dtend = dtstart.AddDays(1);
            }
            else
            {
                dtstart = DateTime.Now.Date;
                dtend = dtstart.AddDays(1);
            }

            int courtId = (int)obj;
            BadmintonEntities bde = new BadmintonEntities();
            IQueryable<CourtSchedule> query = bde.CourtSchedule.Where(p => p.courtId == courtId && p.startTime > dtstart && p.startTime < dtend).OrderBy(p => p.startTime);
            List<CourtSchedule> sd = query.ToList();
            string ret = "";

            DateTime currtime = dtstart;
            foreach (CourtSchedule cs in sd)
            {
                if (cs.startTime>currtime)
                {
                    TimeSpan ts = cs.startTime.Value - currtime;
                    int len = Convert.ToInt32(ts.TotalHours * unit);
                    ret = ret + "<img src='../images/courtschedule/none.PNG' width='" + len.ToString() + "' height='20'>";
                }

                if (cs.endTime > cs.startTime)
                {
                    string fn = "courtordered.PNG";
                    if (cs.CourtScheduleType.name=="培训")
                    {
                        fn = "courtintrain.PNG";
                    }
                    else if(cs.CourtScheduleType.name=="其他")
                    {
                        fn = "courtother.PNG";
                    }
                    TimeSpan ts = cs.endTime.Value - cs.startTime.Value;
                    int len = Convert.ToInt32(ts.TotalHours * unit);
                    ret = ret + "<img src='../images/courtschedule/"+fn+"' width='" + len.ToString() + "' height='20'>";

                }

                currtime = cs.endTime.Value;

            }

            ret = ret + "<img src='../images/courtschedule/none.PNG' width='" + Convert.ToInt32((dtend - currtime).TotalHours * unit).ToString() + "' height='20'>";
 
            return ret;
        }



    }
}